package com.example.qsindusty.jmm_spline.ContralFragment;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.example.qsindusty.jmm_spline.Config.Config;
import com.yixin.ystartlibrary.utils.GeneralPreferencesUtils;
import com.yixin.ystartlibrary.utils.LogUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ControlService extends Service implements Contract.View{
    ControlPresenter mPresenter ;

    public ControlService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.i("onCreate");
        bindPresenter();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent!=null){
            if (intent.getIntExtra(Config.flags,100)==1){
                LogUtils.i("service is yes");
                Intent intent1 = new Intent();
                intent1.setAction("markmap1");
                intent1.putExtra(Config.data,intent.getSerializableExtra(Config.data));
                sendBroadcast(intent);
            }else if (intent.getIntExtra(Config.flags,100)==0){
                LogUtils.i("service is go");
                Observable.interval(0,30, TimeUnit.SECONDS).subscribeOn(Schedulers.io())
                        .subscribe(new Consumer<Long>() {
                            @Override
                            public void accept(Long aLong) throws Exception {
                                mPresenter.getallGPS(GeneralPreferencesUtils.getSharedPreference(getApplicationContext(),Config.token,null));
                            }
                        });

            }
        }
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindPresenter();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void closeLoading() {

    }

    @Override
    public void bindPresenter() {
        if (mPresenter == null) {
            mPresenter = new ControlPresenter();
        }
        mPresenter.bindView(this);
    }

    @Override
    public void unbindPresenter() {
            mPresenter.unbindView();
    }

    @Override
    public void getsuccessful(ControlData controlData) {
        LogUtils.i("succedssf");
        Intent intent = new Intent();
        intent.setAction("markallmap1");
        intent.putExtra(Config.data,controlData);
        sendBroadcast(intent);
    }
    @Override
    public void getsuccessful(GPSData gpsBean) {
        LogUtils.i("succedssf");
        Intent intent = new Intent();
        intent.setAction("markmap1");
        intent.putExtra(Config.data,gpsBean);
        sendBroadcast(intent);
    }
}
